{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 图像平滑处理"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1. 均值滤波"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-26T05:44:48.263691700Z",
     "start_time": "2024-04-26T05:44:47.722842100Z"
    }
   },
   "outputs": [],
   "source": [
    "import cv2 #opencv的缩写为cv2\n",
    "import matplotlib.pyplot as plt # matplotlib库用于绘图展示\n",
    "import numpy as np   # numpy数值计算工具包\n",
    "\n",
    "# 魔法指令，直接展示图，Jupyter notebook特有\n",
    "%matplotlib inline   \n",
    "\n",
    "img = cv2.imread('01_Picture/04_LenaNoise.png')\n",
    "cv2.imshow('img',img)\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-26T05:44:48.877762900Z",
     "start_time": "2024-04-26T05:44:48.230605200Z"
    }
   },
   "outputs": [],
   "source": [
    "# 均值滤波\n",
    "# 简单的平均卷积操作，方框中的值相加，取平均，替换掉中心204的值\n",
    "\n",
    "blur = cv2.blur(img,(3,3)) # (3,3) 为核的大小，通常情况核都是奇数 3、5、7\n",
    "cv2.imshow('blur',blur)\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2. 方框滤波"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-26T05:44:49.471845Z",
     "start_time": "2024-04-26T05:44:48.844680100Z"
    }
   },
   "outputs": [],
   "source": [
    "# 方框滤波\n",
    "# 基本和均值一样，可以选择归一化\n",
    "\n",
    "# 在 Python 中 -1 表示自适应填充对应的值，这里的 -1 表示与颜色通道数自适应一样\n",
    "box = cv2.boxFilter(img,-1,(3,3),normalize=True)  # 方框滤波如果做归一化，得到的结果和均值滤波一模一样\n",
    "cv2.imshow('box',box)\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-26T05:44:50.030848100Z",
     "start_time": "2024-04-26T05:44:49.417843900Z"
    }
   },
   "outputs": [],
   "source": [
    "# 方框滤波\n",
    "# 基本和均值一样，可以选择归一化，容易越界\n",
    "\n",
    "box = cv2.boxFilter(img,-1,(3,3),normalize=False)  # 越界的值取 255\n",
    "cv2.imshow('box',box)\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3. 高斯滤波"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-26T05:44:50.972928100Z",
     "start_time": "2024-04-26T05:44:50.016843600Z"
    }
   },
   "outputs": [],
   "source": [
    "# 高斯函数，越接近均值时，它的概率越大。\n",
    "# 离中心值越近的，它的权重越大，离中心值越远的，它的权重越小。\n",
    "\n",
    "aussian = cv2.GaussianBlur(img,(5,5),1)\n",
    "cv2.imshow('aussian',aussian)\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 4. 中值滤波"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-26T05:44:51.490087200Z",
     "start_time": "2024-04-26T05:44:50.826841800Z"
    }
   },
   "outputs": [],
   "source": [
    "# 中值滤波\n",
    "# 排序后拿中值替代中间元素值的大小\n",
    "\n",
    "median = cv2.medianBlur(img,5)\n",
    "cv2.imshow('median',median)\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 5. 展示所有滤波"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 展示所有的\n",
    "\n",
    "res = np.hstack((blur,aussian,median)) # 矩阵横着拼接\n",
    "#res = np.vstack((blur,aussian,median)) # 矩阵竖着拼接\n",
    "print(res)\n",
    "cv2.imshow('median vs average', res)      \n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "name": "python3",
   "language": "python",
   "display_name": "Python 3 (ipykernel)"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
